/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import pojo.Cuahang;
import util.HibernateUtil;

/**
 *
 * @author sangnguyen
 */
public class CuaHangDAO {
    public static boolean InsertCuaHang(Cuahang obj){
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try{
            session.save(obj);
            tx.commit();
        }
        catch(HibernateException ex){
            tx.rollback();
            System.err.println(ex);
            return false;
        }
        finally{
            session.close();
        }
              
        return true;
    }
    public static boolean UpdateCuaHang(Cuahang obj){
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();        
        try{
            session.update(obj);
            tx.commit();
        }
        catch(HibernateException ex){
            tx.rollback();
            System.err.println(ex);
            return false;
        }
        finally{
           session.close(); 
        }
        return true;
    }
     public static boolean DeleteCuaHang(Cuahang obj){
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try{
            obj.setTinhtrang(0);
            session.save(obj);
            tx.commit();
        }
        catch(HibernateException ex){
            tx.rollback();
            System.err.println(ex);
            return false;
        }
        finally{
            session.close();  
        }
        return true;
    }
     public static List<Cuahang> SelectAllCuaHang(){
         List<Cuahang> lsCuahang = null;
         Session session = HibernateUtil.getSessionFactory().openSession(); 
         try{
             Query query = session.createQuery("from Cuahang ch where ch.tinhtrang=:tinhTrang order by ch.idch asc");
             query.setInteger("tinhTrang", 1);
             lsCuahang = query.list();           
         }
         catch(HibernateException ex){            
            System.err.println(ex);
         }
         finally{
            session.close();  
         }                
         return lsCuahang;
     }
     public static Cuahang GetCuahangID(int id){
         Cuahang obj = new Cuahang();
         Session session = HibernateUtil.getSessionFactory().openSession();    
         try{
             obj = (Cuahang)session.get(Cuahang.class, id);         
         }
         catch(HibernateException ex){
            System.err.println(ex); 
         }
         finally{
            session.close();            
         }
         
         return obj;
     }
}